<template>
	<view>
		<view class="cell-group">
			<u-cell-group :border="false">
				<u-cell-item title="真实姓名" title-width="190" :arrow="false">
					<u-input v-model="info.name" placeholder="请填写真实姓名" type="text" :border="false" />
				</u-cell-item>
				<u-cell-item title="身份证号" title-width="190" :arrow="false">
					<u-input v-model="info.identity" placeholder="请输入身份证号" type="text" :border="false" />
				</u-cell-item>
				<u-cell-item title="支付宝账号" title-width="190" :arrow="false">
					<u-input v-model="info.account" placeholder="请输入支付宝帐号" type="text" :border="false" />
				</u-cell-item>

				<u-cell-item title="提现密码" title-width="190" :arrow="false">
					<u-input v-model="info.pwd" :placeholder="'请输入提现密码'+(is_first==1?'':'（为空则不修改）')" type="password"
						:border="false" maxlength="6" />
				</u-cell-item>

				<u-cell-item title="提现密码确认" title-width="190" :arrow="false" :border="false">
					<u-input v-model="info.pwd_confirm" placeholder="请再次输入提现密码" type="password" maxlength="6" />
				</u-cell-item>
			</u-cell-group>
		</view>

		<view class="pl30 pr30 p-bottom">
			<u-button type="success" size="default" class="w-100" :disabled="disabled" :custom-style="{
					opacity: disabled ?0.7:1,
					height:'88rpx',
					lineHeight:'88rpx',
					background:'linear-gradient(270deg, #55E964 0%, #00AF4E 100%)'
			}" ripple @click="save">完成</u-button>
		</view>

		<u-keyboard mode="number" :tooltip="false" dot-enabled v-model="showKeyboard"></u-keyboard>
	</view>
</template>

<script>
	import * as Api from '@/api/assets/index.js'
	export default {
		data() {
			return {
				is_first: 0, //首次必须填密码
				loading: false,
				info: {
					name: '',
					pwd: '',
					pwd_confirm: '',
				},
				showKeyboard: false,
				keyboardIndex: 0
			}
		},
		computed: {
			disabled() {
				return !this.info.name ||
					!this.info.identity ||
					!this.info.account ||
					this.loading
			}
		},
		methods: {
			async getData() {
				try {
					const res = await Api.account();
					this.info = Object.assign({}, this.info, res.data);
					if (this.is_first == 0 && this.info.name == '') {
						this.is_first = 1;
					}
				} catch (e) {
					this.is_first = 1;
				}
			},
			async save() {
				this.$toast('哈哈哈');
				const {
					pwd,
					pwd_confirm
				} = this.info;
				if ((this.is_first == 1 || pwd) && !/^\d{6}$/.test(pwd)) {
					this.$toast('密码请输入六位数字');
					return;
				}
				if (pwd != pwd_confirm) {
					this.$toast('两次密码不一致，请检查');
					return;
				}
				this.loading = true;
				try {
					const res = await Api.saveAccount(this.info);
					this.$toast(res.message);
					setTimeout(() => {
						this.loading = false;
						uni.navigateBack();
					}, 1000);
				} catch (e) {
					console.log(e.errMsg);
					this.loading = false;
				}
			}
		},
		onLoad(query) {
			this.is_first = query.is_first || 0;
			this.getData();
		}
	}
</script>

<style lang="scss" scoped>
	.cell-group {
		width: 718rpx;
		background: #fff;
		border-radius: 16rpx;
		margin: 20rpx auto 124rpx;
		overflow: hidden;
	}
</style>